<template>
  <div>
    <el-dialog :title="title" v-model="dialogFormVisible" :close-on-click-modal="false" :before-close="closeDialog" v-on:open="loadData" style="margin-top: -75px;">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="num">
              <el-input v-model="form.num"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态" prop="status">
          <el-radio class="radio" v-model="form.status" :label="1">可用</el-radio>
          <el-radio class="radio" v-model="form.status" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-form-item>
        <el-form-item label="菜单权限" prop="menuList">
          <el-checkbox-group v-model="form.menuList">
            <div class="el-table" style="height:235px;overflow: auto;">
            <table width="100%" class="el-table__body">
              <tbody>
              <tr v-for="menu in menuTree">
                <td width="100px" style="vertical-align: top;">
                  <el-checkbox :label="menu.id">{{menu.name}}</el-checkbox>
                </td>
                <td>
                  <table class="el-table__body">
                    <tbody>
                    <template v-for="c in menu.child" >
                      <tr>
                      <td align="left" width="100px">
                        <el-checkbox :label="c.id">{{c.name}}</el-checkbox>
                      </td>
                        <td>
                          <table class="el-table__body">
                            <tbody>
                            <template v-for="c_ in c.child" >
                              <tr>
                                <td align="left" width="100px">
                                  <el-checkbox :label="c_.id">{{c_.name}}</el-checkbox>
                                </td>
                                <td align="left">
                                  <el-checkbox v-for="b in c_.child" :label="b.id">{{b.name}}</el-checkbox>
                                </td>
                              </tr>
                            </template>
                            </tbody>
                          </table>
                        </td>
                      </tr>
                    </template>
                    </tbody>
                  </table>
                </td>
              </tr>
              </tbody>
            </table>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <el-row>
          <el-col :span="24" style="padding:10px;">
            <span style="color:red;float:left;">"*"为必填项。</span>
            <div class="pull-right">
          <el-button type="primary" @click.native.prevent="handleSubmit">{{addBtnTxt}}</el-button>
          <el-button @click.native.prevent="handleReset">重置</el-button>
          <el-button @click.native.prevent="closeDialog">取消</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'
  var model = {name: '', status: 1,num:'', remark: '',menuList:[]};
  export default {
    components: {},
    data () {
      return {
        addBtnTxt:'',
        menuTree:[],
        title: '角色添加', form: Object.assign({}, model),
        rules: {
          name: [
            {required: true, message: '请输入角色名称'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符'}
          ],
          num: [
            {required: true, message: '请输入角色编号'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符'}
          ],
          status:[],
          remark:[],
          menuList:[]
        },
        props: {
          label: 'name',
          children: 'child'
        }
      }
    },
    props: {
      dialogFormVisible: {
        type: Boolean,
        default: false
      },
      id:{
          type:String
      }
    },
    methods: {
      handleReset () {
        this.$refs.form.resetFields()
      },
      handleSubmit (ev) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            http.postData(vm,"/sys/role/save", {json: JSON.stringify(vm.form)},function (resp) {
              var ret = resp.body;
              console.log(ret)
              if (ret.code === '111') {
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
                vm.form = Object.assign({}, model);
                vm.closeDialog();
                vm.$parent.onSubmit();
              } else {
                vm.$notify.error({title: '操作失败', message: ret.msg});
              }
            });
          } else {
            return false
          }
        })
      }, closeDialog(){
        this.$emit('closeDialog');
        this.form = Object.assign({}, model);
      }, loadData(){
        var vm = this;
        if (this.id != undefined && this.id != '') {
          http.postData(vm,"/sys/role/detail/" + this.id, {},function (resp) {
            var ret = resp.body;
            if (ret.code === '111') {
              vm.form = ret.value;
            } else {
              vm.$notify.error({title: '操作失败', message: ret.msg});
            }
          });
        }
        if (vm.id == undefined || vm.id == '') {
          vm.addBtnTxt = '立即创建';
          vm.title = '角色创建';
        } else {
          vm.addBtnTxt = '保存修改';
          vm.title = "角色修改"
        }
      }
    }, created() {
      var vm = this;
      http.postData(vm,"/sys/menu/tree", {},function (resp) {
        var ret = resp.body;
        if (ret.code == '111') {
          vm.menuTree = ret.value;
        }
      });
    }
  }
</script>
